
package formularios;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rulber
 */

public class selec_auditados extends javax.swing.JInternalFrame {

    DefaultTableModel modelo = new DefaultTableModel(new Object[][] {},
            new String[]{"RUC","Nombre","Telefóno","Dirección"})
    {
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    };
    
    String ruc; String nombre; String telefono; String direccion;
    
    public void cargarDatos(String sql)
    {
        try{
            ResultSet rs = conexionBD.s.executeQuery (sql);
            while (rs.next()) {
                Object [] fila = new Object[4];
                for (int i=0;i<4;i++)
                    fila[i] = rs.getObject(i+1);
                modelo.addRow(fila);
            }
        }
        catch(SQLException err)
        {
        }
    }
    
    public int recuperarDatos()
    {
        int fila=datosAuditados.getSelectedRow();
        if(fila!=-1)
        {
           ruc = datosAuditados.getValueAt(fila, 0).toString();
           nombre = datosAuditados.getValueAt(fila, 1).toString();
           try{
               telefono = datosAuditados.getValueAt(fila, 2).toString();
           }
           catch(NullPointerException err){
               telefono="";
           }
           direccion = datosAuditados.getValueAt(fila, 3).toString();
           return 0;
        }
        else
        {
            return 1;
        }
    }
    
    public void limpiarDatos()
    {
        for(int i=datosAuditados.getRowCount()-1;i>=0;i--)
        {
            modelo.removeRow(i);
        }
    }
    
    public void editarDatos()
    {
        int caso;
        caso = recuperarDatos();
        switch(caso)
        {
            case 0:principal.mostrarAuditado_n_e(ruc, nombre, direccion, telefono,2);
                this.dispose();
                break;
            case 1:JOptionPane.showMessageDialog(null,"Debe Seleccionar un Auditado a Editar","SADEC",JOptionPane.INFORMATION_MESSAGE);
                break;
        }
    }
    
    public void recuperarAuditado()
    {
        int caso;
        caso = recuperarDatos();
        switch(caso)
        {
            case 0:principal.mostrarNuevo(nombre, ruc, direccion);
                this.dispose();
                break;
            case 1:JOptionPane.showMessageDialog(null,"Debe Seleccionar un Auditado para Continuar","SADEC",JOptionPane.INFORMATION_MESSAGE);
                break;
        }
    }
    
    public selec_auditados() {
        initComponents();
        int[] anchos = {50,180,40,150};
        for(int i = 0; i < datosAuditados.getColumnCount(); i++) {
            datosAuditados.getColumnModel().getColumn(i).setPreferredWidth(anchos[i]);
        }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        opcionesButons = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        radioButonRUC = new javax.swing.JRadioButton();
        radioButonNombre = new javax.swing.JRadioButton();
        txtBuscar = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        datosAuditados = new javax.swing.JTable();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        opcionesButons.add(radioButonRUC);
        opcionesButons.add(radioButonNombre);

        setClosable(true);
        setTitle("AUDITADOS");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/icono.png"))); // NOI18N

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/auditados.png"))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        radioButonRUC.setSelected(true);
        radioButonRUC.setText("RUC");
        radioButonRUC.setNextFocusableComponent(radioButonNombre);

        radioButonNombre.setText("Nombre");
        radioButonNombre.setNextFocusableComponent(txtBuscar);

        txtBuscar.setNextFocusableComponent(btnNuevo);
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(radioButonRUC, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(radioButonNombre)
                .addGap(18, 18, 18)
                .addComponent(txtBuscar)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(radioButonRUC)
                .addComponent(radioButonNombre)
                .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        datosAuditados.setModel(modelo);
        datosAuditados.setNextFocusableComponent(btnNuevo);
        jScrollPane1.setViewportView(datosAuditados);

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/auditados+.png"))); // NOI18N
        btnNuevo.setToolTipText("Nuevo");
        btnNuevo.setNextFocusableComponent(btnEditar);
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        btnNuevo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnNuevoKeyPressed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/auditadose.png"))); // NOI18N
        btnEditar.setToolTipText("Editar");
        btnEditar.setNextFocusableComponent(btnAceptar);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        btnEditar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnEditarKeyPressed(evt);
            }
        });

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/aceptar.png"))); // NOI18N
        btnAceptar.setToolTipText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        btnAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnAceptarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(101, 101, 101)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(91, 91, 91)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(121, 121, 121))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 626, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnEditar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        principal.mostrarAuditado_n_e(2);
        this.dispose();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        editarDatos();
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnNuevoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnNuevoKeyPressed
        if(evt.getKeyCode() == 10)
        {
            principal.mostrarAuditado_n_e(2);
            this.dispose();
        }
    }//GEN-LAST:event_btnNuevoKeyPressed

    private void btnEditarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnEditarKeyPressed
        if(evt.getKeyCode() == 10)
        {
            editarDatos();
        }
    }//GEN-LAST:event_btnEditarKeyPressed

    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        String sql="";
        limpiarDatos();
        if(radioButonRUC.isSelected())
        {
            sql="select * from auditado where RUC like '%"+txtBuscar.getText()+"%'";
        }
        if(radioButonNombre.isSelected())
        {
            sql="select * from auditado where nombre like '%"+txtBuscar.getText()+"%'";
        }
        cargarDatos(sql);
    }//GEN-LAST:event_txtBuscarKeyReleased

    private void btnAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnAceptarKeyPressed
        recuperarAuditado();
    }//GEN-LAST:event_btnAceptarKeyPressed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        recuperarAuditado();
    }//GEN-LAST:event_btnAceptarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JTable datosAuditados;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.ButtonGroup opcionesButons;
    private javax.swing.JRadioButton radioButonNombre;
    private javax.swing.JRadioButton radioButonRUC;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables
}
